
package com.scs.application.modules.cert.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.exceptions.BusinessException;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.cert.entity.Certificate;
import com.scs.application.modules.cert.entity.CertificateHospitalBusiness;
import com.scs.application.modules.cert.mapper.CertificateHospitalBusinessMapper;
import com.scs.application.modules.cert.mapper.CertificateMapper;
import com.scs.application.modules.cert.request.CertificateQueryRequest;
import com.scs.application.modules.cert.service.CertificateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * 预定义资质 service 实现
 *
 */
@Slf4j
@Service
public class CertificateServiceImpl extends BaseServiceImpl<CertificateMapper, Certificate> implements CertificateService {

    @Autowired
    protected CertificateMapper certificateMapper;

    @Autowired
    protected CertificateHospitalBusinessMapper certificateHospitalBusinessMapper;

    @Override
    public List<Certificate> list(CertificateQueryRequest request) {
        List<Certificate> list = this.list(Wrappers.<Certificate>query()
                .like(StringUtils.isNotBlank(request.getCode()), "code", request.getCode())
                .like(StringUtils.isNotBlank(request.getName()), "name", request.getName())
                .eq(StringUtils.isNoneBlank(request.getBusinessTypeCode()), "business_type_code", request.getBusinessTypeCode())
                .eq(request.getNeedExpiryDate() != null, "need_expiry_date", request.getNeedExpiryDate())
                .orderByDesc("code")
        );
        return list;
    }

    @Override
    public IPage<Certificate> page(CertificateQueryRequest request) {
        IPage page = this.page(request.getPage(), Wrappers.<Certificate>query()
                .like(StringUtils.isNotBlank(request.getCode()), "code", request.getCode())
                .like(StringUtils.isNotBlank(request.getName()), "name", request.getName())
                .eq(StringUtils.isNoneBlank(request.getBusinessTypeCode()), "business_type_code", request.getBusinessTypeCode())
                .eq(request.getNeedExpiryDate() != null, "need_expiry_date", request.getNeedExpiryDate())
                .orderByDesc("code")
        );
        return page;
    }

    @Override
    public IPage<Certificate> pageHospitalNotExisted(CertificateQueryRequest request) {
        request.setHospitalId(UserUtils.getHospitalId());
        return certificateMapper.pageHospitalNotExist(request.getPage(), request);
    }


    @Override
    public boolean saveOrUpdate(Certificate entity) {
        entity.setName(StringUtils.trim(entity.getName()));
        BusinessException.throwErrorIf(
                this.count(Wrappers.<Certificate>query()
                        .ne(!entity.isNewRecord(),"id", entity.getId())
                        .eq("business_type_code", entity.getBusinessTypeCode())
                        .eq("code", entity.getCode())
                        .eq("name", entity.getName())
                ) > 0
                , "资质类型、资质编码、资质名称必须唯一"
        );
        return super.saveOrUpdate(entity);
    }

    @Override
    public boolean removeCascadeBatchByIds(Collection<? extends Serializable> idList) {
        List<CertificateHospitalBusiness> certificateHospitalBusinessList = certificateHospitalBusinessMapper.selectList(Wrappers.<CertificateHospitalBusiness>query().in("certificate_id", idList));
        if (ObjectUtil.length(certificateHospitalBusinessList) > 0) {
            Certificate find = this.getById(certificateHospitalBusinessList.get(0).getCertificateId());
            BusinessException.throwError("资质【{}】已添加到院方资质定义中，不允许删除",find.getName());
        }
        return super.removeCascadeBatchByIds(idList);
    }
}
